package com.magiccompass.alipay.barcode.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magiccompass.alipay.barcode.entity.AlipayBarcodePaymentResponse;
import com.magiccompass.alipay.barcode.entity.MerchantBankAccount;
import com.magiccompass.alipay.barcode.entity.MerchantEmails;
import com.magiccompass.alipay.barcode.entity.MerchantGroup;
import com.magiccompass.alipay.barcode.entity.MerchantInfo;
import com.magiccompass.alipay.barcode.entity.MerchantTransaction;
import com.magiccompass.alipay.barcode.entity.MerchantTransactionDetail;
import com.magiccompass.alipay.barcode.entity.UserInfo;
import com.magiccompass.alipay.barcode.entity.ViewMerchantTransactionStatusRecords;

@Service("merchantBusinessService")
@Transactional
public class MerchantBusinessServiceImpl implements MerchantBusinessService {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	public MerchantBusinessServiceImpl(){};
	
	@Override
	public void save(Object entity){
		sessionFactory.getCurrentSession().saveOrUpdate(entity);
	}
	
	@Override
	public void save(MerchantTransaction transaction){
		sessionFactory.getCurrentSession().save(transaction);
	}
	
	@Override
	public List<MerchantTransaction> getMerchantTransactionListByTime(long fromDate, long toDate, String merchantId){
		Session session = sessionFactory.getCurrentSession();
		Query query = null;
		String hql = null;
		if("%".equals(merchantId)){
			hql = "FROM MerchantTransaction mt WHERE mt.createTime>=:fromDate and mt.createTime<=:toDate order by id desc";
			query = session.createQuery(hql);
		}else{
			hql = "FROM MerchantTransaction mt WHERE mt.merchant.id =:merchantId and mt.createTime>=:fromDate and mt.createTime<=:toDate order by id desc";
			query = session.createQuery(hql);
			query.setParameter("merchantId",Integer.parseInt(merchantId));
		}
		query.setParameter("fromDate",new Date(fromDate));
		query.setParameter("toDate",new Date(toDate));
		List<MerchantTransaction> result = (List<MerchantTransaction>)query.list();
		return result;
	}
	
	@Override
	public List<MerchantTransaction> getMerchantTransactionListByUpdateTime(long fromDate, long toDate, Integer merchantId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantTransaction mt WHERE mt.merchant.id = :merchantId and mt.updateTime>=:fromDate and mt.updateTime<=:toDate";
		Query query = session.createQuery(hql);
		query.setParameter("merchantId",merchantId);
		query.setParameter("fromDate",new Date(fromDate));
		query.setParameter("toDate",new Date(toDate));
		List<MerchantTransaction> result = (List<MerchantTransaction>)query.list();
		return result;
	}
	
	@Override
	public MerchantInfo findMerchantById(Integer id){
		Session session = sessionFactory.getCurrentSession();
		return (MerchantInfo)session.get(MerchantInfo.class, id);
	}
	
	@Override
	public List<MerchantInfo> findAllActiveMerchant(){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantInfo mi WHERE mi.delFlag = 'F' order by mi.id desc";
		Query query = session.createQuery(hql);
		List<MerchantInfo> result = (List<MerchantInfo>)query.list();
		return result;
	}
	
	@Override
	public MerchantTransaction findMerchantTransactionById(Integer id){
		Session session = sessionFactory.getCurrentSession();
		return (MerchantTransaction)session.get(MerchantTransaction.class, id);
		
	}
	
	@Override
	public List<MerchantTransactionDetail> findMerchantTransactionOrderDetailById(Integer id){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantTransactionDetail mtd WHERE mtd.merchantTransaction.id = :inputId order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",id);
		List<MerchantTransactionDetail> results = query.list();		
		return results;		
	}
	
	@Override
	public List<MerchantInfo> findMerchantByNameAndDelFlag(String queryName, String delFlag){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantInfo mi WHERE mi.merchantName like :inpuQueryName and mi.delFlag like :inputDelFlag order by mi.id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inpuQueryName","%"+queryName+"%");
		query.setParameter("inputDelFlag",delFlag);
		List<MerchantInfo> result = (List<MerchantInfo>)query.list();
		return result;
	}
	
	@Override
	public List<MerchantBankAccount> findAllActiveMerchantBankAccount(Integer merchantId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantBankAccount mba WHERE mba.merchant.id = :inputId order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",merchantId);
		List<MerchantBankAccount> results = query.list();		
		return results;
	}
	
	@Override
	public List<AlipayBarcodePaymentResponse> findMerchantTransactionPaymentResponse(Integer mtId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM AlipayBarcodePaymentResponse abpr WHERE abpr.merchantTransaction.id = :inputId order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",mtId);
		List<AlipayBarcodePaymentResponse> results = query.list();		
		return results;
	}
	
	@Override
	public List<UserInfo> findMerchantOperatorByNameAndMerchantIdAndDelFlag(String operatorName,String merchantId, String delFlag){
		Session session = sessionFactory.getCurrentSession();
		Query query;
		
		if(!"%".equals(merchantId)){
			String hql = "FROM UserInfo ui WHERE ui.roleId in (2,3) and ui.userName like :operatorName and ui.delFlag like :delFlag and ui.merchant.id=:merchantId order by id desc";				
			query = session.createQuery(hql);
			query.setParameter("operatorName",operatorName);
			query.setParameter("delFlag",delFlag);
			query.setParameter("merchantId",Integer.parseInt(merchantId));
		}else{
			String hql = "FROM UserInfo ui WHERE ui.roleId in (2,3) and ui.userName like :operatorName and ui.delFlag like :delFlag";				
			query = session.createQuery(hql);
			query.setParameter("operatorName",operatorName);
			query.setParameter("delFlag",delFlag);
		}
		List<UserInfo> results = query.list();		
		return results;	
	}	
	@Override
	public UserInfo findMerchantOperatorById(Integer id){
		Session session = sessionFactory.getCurrentSession();
		return (UserInfo)session.get(UserInfo.class, id);
	}
	@Override
	public List<MerchantTransaction> getMerchantTransactionListWithAlipayStatusOnlyByUpdateTime(long fromDate, long toDate, Integer merchantId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantTransaction mt WHERE mt.status in ('S','R','C','E') and mt.merchant.id = :merchantId and mt.updateTime>=:fromDate and mt.updateTime<=:toDate order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("merchantId",merchantId);
		query.setParameter("fromDate",new Date(fromDate));
		query.setParameter("toDate",new Date(toDate));
		List<MerchantTransaction> result = (List<MerchantTransaction>)query.list();
		return result;
	}
	@Override
	public List<MerchantEmails> findAllActiveMerchantSupportEmails(Integer merchantId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantEmails me WHERE me.merchant.id = :inputId and me.delFlag='F' order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",merchantId);
		List<MerchantEmails> results = query.list();		
		return results;
	}
	@Override
	public void removeAllActiveMerchantSupportEmails(Integer merchantId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "update MerchantEmails me set me.delFlag='T' WHERE me.merchant.id = :inputId and me.delFlag='F' order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",merchantId);
		query.executeUpdate();
	}	
	@Override
	public List<ViewMerchantTransactionStatusRecords> findTransactionAllStatus(Integer mtId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM ViewMerchantTransactionStatusRecords v WHERE v.transactionId = :inputId order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",mtId);
		List<ViewMerchantTransactionStatusRecords> results = query.list();		
		return results;	
	}
	@Override
	public List<MerchantGroup> findMerchantGroupByName(String queryName){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantGroup mg WHERE mg.groupName like :inpuQueryName order by mg.id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inpuQueryName","%"+queryName+"%");
		List<MerchantGroup> result = (List<MerchantGroup>)query.list();
		return result;
	}
	@Override
	public List<MerchantGroup> findAllMerchantGroup(){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantGroup mg order by mg.id desc";
		Query query = session.createQuery(hql);
		List<MerchantGroup> result = (List<MerchantGroup>)query.list();
		return result;
	}
	@Override
	public MerchantGroup findMerchantGroupById(Integer merchantGroupId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantGroup mg where mg.id=:inputId";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",merchantGroupId);
		List<MerchantGroup> result = (List<MerchantGroup>)query.list();
		if(result != null && result.size()>0){
			return result.get(0);
		}else{
			return null;
		}
	}
	@Override
	public List<MerchantInfo> findMerchantByGroupId(Integer mgId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantInfo mi WHERE mi.merchantGroup.id =:mgId order by mi.id desc";
		Query query = session.createQuery(hql);
		query.setParameter("mgId",mgId);
		List<MerchantInfo> result = (List<MerchantInfo>)query.list();
		return result;
	}
	@Override
	public List<UserInfo> findMerchantAdminOperatorByNameAndDelFlag(String operatorName, String delFlag){
		Session session = sessionFactory.getCurrentSession();
		Query query;		
		
		String hql = "FROM UserInfo ui WHERE ui.roleId = 4 and ui.userName like :operatorName and ui.delFlag like :delFlag";				
		query = session.createQuery(hql);
		query.setParameter("operatorName",operatorName);
		query.setParameter("delFlag",delFlag);
		
		List<UserInfo> results = query.list();		
		return results;	
	}
	@Override
	public List<MerchantTransaction> getMerchantTransactionListFromGroupByTime(long fromDate, long toDate, String merchantId, String merchantGroupIds){
		Session session = sessionFactory.getCurrentSession();
		Query query = null;
		String hql = null;
		if("%".equals(merchantId)){
			hql = "FROM MerchantTransaction mt WHERE mt.createTime>=:fromDate and mt.createTime<=:toDate and mt.merchant.id in "+merchantGroupIds+" order by id desc";
			query = session.createQuery(hql);
		}else{
			hql = "FROM MerchantTransaction mt WHERE mt.merchant.id =:merchantId and mt.createTime>=:fromDate and mt.createTime<=:toDate order by id desc";
			query = session.createQuery(hql);
			query.setParameter("merchantId",Integer.parseInt(merchantId));
		}
		query.setParameter("fromDate",new Date(fromDate));
		query.setParameter("toDate",new Date(toDate));
		List<MerchantTransaction> result = (List<MerchantTransaction>)query.list();
		return result;
	}
}